app.controller("z-businessTrip-c", [ '$scope','$rootScope','$z-http','$sce','z-request-s','z-attendance-s','z-edit-menu-s','z-requestApproval-s','z-role-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,AttendanceService,MenuService,RequestApprovalService,RoleService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";		  
		$scope.viewModel.attendanceState=""       //考勤状态
		var now = new Date();                     //当前日期     
		var nowMonth = now.getMonth();            //当前月     
		var nowYear=$filter('date')(now,'yyyy');  //当前年
		//获取当月开始日期
		var monthStartDate = $filter("date")(new Date(nowYear, nowMonth, 1), "yyyy/MM/dd");
		//获取当前日期
        var dayEndDate= $filter("date")(now, "yyyy/MM/dd");
		$scope.viewModel.requestStartDate=new Date(monthStartDate);  //起止日期
		$scope.viewModel.requestEndDate=new Date(dayEndDate);	  	  //终止日期
		//日期控件
		$scope.viewModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		//日期控件
		$scope.viewModel.format = "yyyy/MM/dd";
		//日期启动开关
		$scope.viewModel.startDateOpened = false;
		$scope.viewModel.endDateOpened = false;
		$scope.viewModel.editStartDateOpened = false;
		$scope.viewModel.editEndDateOpened = false;
		
		$scope.editModel = {};
		$scope.editModel.request={};
		$scope.editModel.request.startToTime=null;
		$scope.editModel.title="";
		$scope.editModel.startToTimeShow = 1;
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'businessTrip'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		
		//起止日期选择
		$scope.viewModel.startDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.viewModel.startDateOpened = !$scope.viewModel.startDateOpened;
		};
		
		//起止日期选择
		$scope.viewModel.endDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.viewModel.endDateOpened = !$scope.viewModel.endDateOpened;
		};
		
		$scope.viewModel.editStartDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			if($scope.viewModel.editEndDateOpened){
				$scope.viewModel.editEndDateOpened=false;
			}
			$("#bStartTimehsdate").hide();
			$("#bStartToTimehsdate").hide();
			$("#bEndTimehsdate").hide();
			$scope.viewModel.editStartDateOpened = !$scope.viewModel.editStartDateOpened;
		};
		
		$scope.viewModel.editEndDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			if($scope.viewModel.editStartDateOpened){
				$scope.viewModel.editStartDateOpened=false;
			}
			$("#bStartTimehsdate").hide();
			$("#bStartToTimehsdate").hide();
			$("#bEndTimehsdate").hide();
			$scope.viewModel.editEndDateOpened = !$scope.viewModel.editEndDateOpened;
		};
		
		//选择开始时间
		$scope.viewModel.startTimeClick=function(){
			$("#bStartTimehsdate").show();
			$("#bStartToTimehsdate").hide();
			$("#bEndTimehsdate").hide();
			if($scope.viewModel.editStartDateOpened){
				$scope.viewModel.editStartDateOpened=false;
			}
			if($scope.viewModel.editEndDateOpened){
				$scope.viewModel.editEndDateOpened=false;
			}
		};
		
		//选择至
		$scope.viewModel.startToTimeClick=function(){
			$("#bStartTimehsdate").hide();
			$("#bStartToTimehsdate").show();
			$("#bEndTimehsdate").hide();
			if($scope.viewModel.editStartDateOpened){
				$scope.viewModel.editStartDateOpened=false;
			}
			if($scope.viewModel.editEndDateOpened){
				$scope.viewModel.editEndDateOpened=false;
			}
		};
		
		$scope.viewModel.endTimeClick=function(){
			$("#bStartTimehsdate").hide();
			$("#bStartToTimehsdate").hide();
			$("#bEndTimehsdate").show();
			if($scope.viewModel.editStartDateOpened){
				$scope.viewModel.editStartDateOpened=false;
			}
			if($scope.viewModel.editEndDateOpened){
				$scope.viewModel.editEndDateOpened=false;
			}
		};
		
		$scope.viewModel.getRequestHours=function(startToTimeShow){
			if($scope.editModel.request.endDate!=null 
					&& $scope.editModel.request.startDate!=null
					&& $scope.editModel.request.startTime!=null
					&& $scope.editModel.request.endDate!=null
					&& $scope.editModel.request.endTime!=null
					&& $scope.editModel.request.startDate!=null){
						if($scope.editModel.startToTimeShow==0){
							if($scope.editModel.request.startToTime==""
								|| $scope.editModel.request.startToTime==null){
								$scope.editModel.request.hours = 0;
								$scope.editModel.hours=$scope.viewModel.toHourMinute($scope.editModel.request.hours);
								$('#bhours').val($scope.editModel.hours);
								return;
							}else{
								//判断结束时间是否小于开始时间
								if($scope.editModel.request.startTime>=$scope.editModel.request.startToTime){
									$scope.editModel.request.hours = 0;
									$scope.editModel.hours=$scope.viewModel.toHourMinute($scope.editModel.request.hours);
									$('#ghours').val($scope.editModel.hours);
									return;
								}
							}
						}
						$scope.editModel.hours=null;
						$('#bhours').val($scope.editModel.hours);
						Service.getRequestHours($scope.editModel.request,
								function(data){
									$scope.editModel.request.hours = data;
									$scope.editModel.hours=$scope.viewModel.toHourMinute($scope.editModel.request.hours);
									$('#bhours').val($scope.editModel.hours);
								},function(error) {
									$rootScope.alertError(error.fault.message);
									$scope.editModel.request.hours = 0;
									$scope.editModel.hours=$scope.viewModel.toHourMinute($scope.editModel.request.hours);
									$('#bhours').val($scope.editModel.hours);
								});
			}else{
				$scope.editModel.request.hours=0;
				$scope.editModel.hours=$scope.viewModel.toHourMinute($scope.editModel.request.hours);
				$('#bhours').val($scope.editModel.hours);
			}
		};
		
		// 将分钟数量转换为小时和分钟字符串
		$scope.viewModel.toHourMinute=function(minutes){
			if(minutes==null){
				return "";
			}
			return (Math.floor(minutes/60).toFixed(0) + "小时" + Math.floor(minutes%60).toFixed(0) + "分钟" );
		};
		
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getAwayRequestList({
				pageNo:$scope.viewModel.rowCount,
				pageCount:$scope.viewModel.perPage,
				requestStartDate:$scope.viewModel.requestStartDate,
				requestEndDate:$scope.viewModel.requestEndDate,
				requestType:8
			},
			function(data){
				$scope.viewModel.dataList = data.requestBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		$scope.viewModel.bindData();
		
		$scope.viewModel.bindRoleData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			RoleService.getRoleListByEmployeeId({},
			function(data){
				$scope.viewModel.roleList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		$scope.viewModel.bindRoleData();
		
		$scope.viewModel.validForm= function(isValid) {
            if (!isValid) {
                return;
            }else{
            	if($scope.viewModel.requestStartDate>$scope.viewModel.requestEndDate){
            		$scope.form.requestEndDate.$invalid=true;
            	}else{
            		$scope.form.requestEndDate.$invalid=false;
            		$scope.viewModel.rowCount=1;
                	$scope.viewModel.bindData();
            	}
            }
        };
        
        //格式化验证
        $scope.viewModel.escapeRegExp=function(string){
	        var pattern=/^([0-9]|[0-1][0-9]|2[0-3]):(2[0-9]|[0-5][0-9])$/;
	        var flag = pattern.test(string);
	        return flag;
        };
        
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
			$('#bStartTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
					if($scope.viewModel.escapeRegExp($('#bStartTime').val())){
						$scope.editForm.bStartTime.$valid=true;
						$scope.editForm.bStartTime.$dirty=true;
						$scope.editForm.bStartTime.$invalid=false;
						$scope.editModel.request.startTime=$('#bStartTime').val();
						$scope.viewModel.dateChange(false);
					}else{
						$('#bhours').val($scope.viewModel.toHourMinute(0));
						$scope.editModel.request.hours=0;
					}
				}	
			});
			$('#bEndTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
					if($scope.viewModel.escapeRegExp($('#bEndTime').val())){
						$scope.editForm.bEndTime.$valid=true;
						$scope.editForm.bEndTime.$dirty=true;
						$scope.editForm.bEndTime.$invalid=false;
						$scope.editModel.request.endTime=$('#bEndTime').val();
						$scope.viewModel.dateChange(false);
					}else{
						$('#bhours').val($scope.viewModel.toHourMinute(0));
						$scope.editModel.request.hours=0;
					}
				}	
			});
			$('#bStartToTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
					if($scope.viewModel.escapeRegExp($('#bStartToTime').val())){
						$scope.editForm.bStartToTime.$valid=true;
						$scope.editForm.bStartToTime.$dirty=true;
						$scope.editForm.bStartToTime.$invalid=false;
						$scope.editModel.request.startToTime=$('#bStartToTime').val();
						$scope.viewModel.dateChange(false);
					}else{
						$('#bhours').val($scope.viewModel.toHourMinute(0));
						$scope.editModel.request.hours=0;
					}
				}	
			});
		};
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
			$scope.editModel.request={};
			$scope.editModel.request.startToTime=null;
			$scope.editModel.startToTimeShow = 1;
		};
		//新增
		$scope.viewModel.add = function(request){
			$scope.editModel.title="新增出差申请";
			$scope.editModel.type = true;
			$scope.editModel.request={};
			$scope.editModel.request.startToTime=null;
			$scope.editModel.request.startDate=null;
			$scope.editModel.request.endDate=null;
			$scope.editModel.request.startTime=null;
			$scope.editModel.request.endTime=null;
			$scope.editModel.hours=null;
			$scope.editModel.request.requestType=8;
			$scope.viewModel.dirty();
			$scope.viewModel.load();
		};
		
		//修正
		$scope.viewModel.update=function(request){
			$scope.editModel.title="修改出差申请";
			$scope.editModel.type = false;
			$scope.editModel.request=angular.copy(request);
			$scope.editModel.request.startDate=new Date(request.startDate);
			$scope.editModel.request.endDate=new Date(request.endDate);
			var a = $scope.editModel.request.hours;
			$scope.editModel.hours=$scope.viewModel.toHourMinute(request.hours);
			$scope.viewModel.dirty();
			$scope.viewModel.load();
			if(request.startDate < request.endDate){
				$scope.editModel.startToTimeShow = "0";
			}else{
				$scope.editModel.startToTimeShow != "0";
			}
		};
		
		$scope.viewModel.dirty=function(){
			//验证不能为空
			$scope.editForm.startDate.$dirty=false;
			$scope.editForm.bStartTime.$dirty=false;
			$scope.editForm.endDate.$dirty=false;
			$scope.editForm.bEndTime.$dirty=false;
			$scope.editForm.description.$dirty=false;
		};
		
		//提交
		$scope.viewModel.submit=function(request){
			$rootScope.alertConfirm("确定是否提交吗？",function() {
				$scope.editModel.request=angular.copy(request);
				$scope.editModel.request.approvalState=1;
				$scope.editModel.request.startDate=new Date(request.startDate);
				$scope.editModel.request.endDate=new Date(request.endDate);
				$scope.viewModel.isLoading = true;
				Service.updateRequest($scope.editModel.request,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("提交成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			});
		};
		//验证
		$scope.viewModel.requiredForm= function() {
			if (!$scope.editForm.startDate.$valid
					|| !$scope.editForm.bStartTime.$valid
					|| !$scope.editForm.endDate.$valid
					|| !$scope.editForm.bEndTime.$valid
					|| !$scope.editForm.description.$valid) {
            	if(!$scope.editForm.startDate.$dirty){
            		$scope.editForm.startDate.$dirty=true;
            	}
            	if(!$scope.editForm.bStartTime.$dirty){
            		$scope.editForm.bStartTime.$dirty=true;
            	}
            	if(!$scope.editForm.endDate.$dirty){
            		$scope.editForm.endDate.$dirty=true;
            	}
            	if(!$scope.editForm.bEndTime.$dirty){
            		$scope.editForm.bEndTime.$dirty=true;
            	}
            	if(!$scope.editForm.description.$dirty){
            		$scope.editForm.description.$dirty=true;
            	}
    			if($scope.editModel.startToTimeShow==0){
    				if($scope.editModel.request.startToTime ==null
							|| $scope.editModel.request.startToTime ==""){
    					$scope.editForm.bStartToTime.$dirty=true;
						$scope.editForm.bStartToTime.$invalid=true;
						return true;
					}else{
						$scope.editForm.bStartToTime.$invalid=false;
					}
    			}
                return true;
            }else{
            	if($scope.editModel.startToTimeShow==0){
					if($scope.editModel.request.startToTime ==null
							|| $scope.editModel.request.startToTime ==""){
						$scope.editForm.bStartToTime.$dirty=true;
						$scope.editForm.bStartToTime.$invalid=true;
						return true;
					}else{
						$scope.editForm.bStartToTime.$invalid=false;
					}
    			}
            	if($scope.editModel.request.startDate>$scope.editModel.request.endDate){
            		$scope.editForm.endDate.$invalid=true;
            		return true;
            	}
            	if($scope.editModel.request.startDate < $scope.editModel.request.endDate){
                	if($scope.editModel.startToTimeShow==0){
                		if($scope.editModel.request.startToTime ==null
    							|| $scope.editModel.request.startToTime ==""){
                    		$rootScope.alertWarning("未填写，结束时间，请确认。");
                    		$scope.editForm.bStartToTime.$dirty=true;
    						$scope.editForm.bStartToTime.$invalid=true;
                    		return true;
                    	}
    	            	if($scope.editModel.request.startTime>=$scope.editModel.request.startToTime){
    	            		$rootScope.alertWarning("结束时间不能小于开始时间。");
    						$scope.editForm.bStartToTime.$dirty=true;
    						$scope.editForm.bStartToTime.$invalid=true;
    	            		return true;
    	            	}
    	            	$scope.editForm.bStartToTime.$invalid=false;
                	}
				}
               	if($scope.editModel.request.hours<=0){
            		$rootScope.alertWarning("出差时长不能为0小时，请确认！");
            		$scope.editForm.bEndTime.$invalid=true;
                	return true;
            	}
				if($scope.editModel.request.hours<60){
					$rootScope.alertWarning("出差低于一小时不允许申请。");
					return true;
				}
        		$scope.editForm.endDate.$invalid=false;
            	return false;
            }
        };
        
		$scope.viewModel.saveDate = function(){
			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
				Service.saveRequest($scope.editModel.request,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("新增成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}else{
				Service.updateRequest($scope.editModel.request,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("修改成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}
		}
		
		//保存
		$scope.viewModel.save = function(){
			$scope.editModel.request.approvalState=0;
			if($scope.viewModel.requiredForm()){
				return;
			}
			$rootScope.alertConfirm("确定保存吗？",function() {
				$scope.viewModel.saveDate();
			});
		};
		
		//提交
        $scope.viewModel.saveSubmit = function(){
        	$scope.editModel.request.approvalState=1;
			if($scope.viewModel.requiredForm()){
				return;
			}
        	$rootScope.alertConfirm("确定保存并提交吗？",function() {
				$scope.viewModel.saveDate();
			});
        };
        
		//删除
		$scope.viewModel.del=function(request){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteRequest({
					requestId:request.requestId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
		
		//撤回
		$scope.viewModel.withdraw=function(request){
			$rootScope.alertConfirm("确定撤回吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.getBackRequest({
					requestId:request.requestId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("撤回成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};

		//时间框失去焦点
		$scope.viewModel.dateChange=function(type){
			if($scope.editModel.request.startDate!=null
					&& $scope.editModel.request.endDate!=null){
				if($scope.editModel.request.startDate < $scope.editModel.request.endDate){
					//获取基本信息
					AttendanceService.getAttendanceCount({
						attendanceDate:$scope.editModel.request.startDate,
					},
					function(data){
						$scope.editModel.startToTimeShow = data;
					},function(error) {
						//$rootScope.alertError(error.fault.message);
					});
				}else{
					$scope.editModel.startToTimeShow = 1;
					$scope.editModel.request.startToTime = "";
				}
				$scope.viewModel.getRequestHours($scope.editModel.startToTimeShow);
			}
		};
		
		//按钮是否可用
		$scope.viewModel.isDisabled=function(request){
			if(request.approvalState==0){
        		return false;
        	}
        	return true;
		};
		/*  状态详情 */
		$scope.viewApprovalRecordModel = {};
		// 点击列表申请状态
		$scope.viewModel.selecttApprovalRecord = function(request){
			if(request.approvalState != 0){
				$scope.viewApprovalRecordModel.show = true;
				$scope.viewApprovalRecordModel.isLoading=true;
				$scope.viewApprovalRecordModel.title = "查看审批过程";
				//获取基本信息
				RequestApprovalService.getApprovalRecordInfoById({
					requestId:request.requestId,
				},
				function(data){
					$scope.viewApprovalRecordModel.dataList = data;
					$scope.viewApprovalRecordModel.isLoading=false;
				},function(error) {
					$rootScope.alertError(error.fault.message);
					$scope.viewApprovalRecordModel.isLoading=false;
				});
			}
		}
		//关闭加载
		$scope.viewApprovalRecordModel.unLoad=function(){
			$scope.viewApprovalRecordModel.show=false;
		};
		
		$scope.viewModel.selected=function(role){
			if(role.isRole==1){
				$scope.editModel.request.roleId=role.roleId;
				return true;
			}
			return false;
		};
} ]);